package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 角色分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param roleDto
     * @return
     */
    @Override
    public PageResponse<RoleVo> selectByPage(Integer pageNum, Integer pageSize, RoleDto roleDto) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> result = roleMapper.selectByPage(roleDto);
        PageResponse<RoleVo> pageResult = PageResponse.of(result, RoleVo.class);
        return pageResult;

    }

    /**
     * 新增角色
     *
     * @param roleDto
     */
    @Override
    public void addRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 修改角色
     *
     * @param roleDto
     */
    @Override
    public void updateRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        //要禁用 先看是否已经绑定用户
        if (roleDto.getDataState() != null &&roleDto.getDataState().equals("1")) {
            Integer count = userRoleMapper.selectByRoleId(roleDto.getId());
            if(count != 0){
                throw new BaseException(BasicEnum. ROLE_DISTRIBUTED);
            }
        }
        //判断是否包含菜单权限修改
       String[] no =  roleDto.getCheckedResourceNos();
        List<RoleResource> list = new ArrayList<>();
        if (no != null) {
            for (String s : no) {
                //拼接每一个资源 + 角色Id
                RoleResource roleResource = new RoleResource();
                roleResource.setResourceNo(s);
                roleResource.setRoleId(roleDto.getId());
                roleResource.setDataState("0");
                list.add(roleResource);
            }
            roleResourceMapper.batchInsert(list);
        }
        roleMapper.updateRole(role);
    }

    /**
     * 根据角色查询所属资源
     *
     * @param roleId
     * @return
     */
    @Override
    public List<String> findCheckedResources(Long roleId) {
        List<String> list = roleResourceMapper.selectByRoleId(roleId);
        return list;
    }

    /**
     * 删除角色
     *
     * @param roleId
     */
    @Transactional
    public void deleteRole(Long roleId) {
        //先判断角色状态
        RoleVo role = roleMapper.selectByRoleId(roleId);
        if (role.getDataState().equals("0")) {
            throw new BeanException("启用状态下不可删除");
        }
        //删除关联表
        roleResourceMapper.deleteByRoleId(roleId);
        //删除角色
        roleMapper.deleteByPrimaryKey(role.getId());
    }

    @Override
    public List<RoleVo> roleList() {
        return  roleMapper.list();

    }
}
